class EventManager {
    static eventMap = new Map();

    static on(name, callback) {
        if (!this.eventMap.has(name)) {
            this.eventMap.set(name, []);
        }
        this.eventMap.get(name).push(callback);
    }

    static off(name, callback = null) {
        if (!this.eventMap.has(name)) return;
        
        if (callback) {
            // 移除特定回调
            const callbacks = this.eventMap.get(name);
            const index = callbacks.indexOf(callback);
            if (index > -1) {
                callbacks.splice(index, 1);
            }
            if (callbacks.length === 0) {
                this.eventMap.delete(name);
            }
        } else {
            // 移除所有回调
            this.eventMap.delete(name);
        }
    }

    static emit(name, ...args) {
        const callbacks = this.eventMap.get(name);
        if (callbacks) {
            callbacks.forEach(callback => {
                callback.apply(null, args);
            });
        }
    }
}

export default EventManager;
